<?php

namespace serv\queue\oauth;

use common\IpTrait;
use Haistar\ShopeePhpSdk\client\ShopeeApiConfig;
use Haistar\ShopeePhpSdk\request\general\GeneralApiClient;
use DTS\eBaySDK\OAuth\Services;
use DTS\eBaySDK\OAuth\Types;
use serv\rapi\spapi\base;
use Meli\Api\OAuth20Api;
use GuzzleHttp\Client;
use Exception;
use L;
use of;

/*
 * 统一刷新授权信息队列 by@ 阿科 2021-10-28
 *
 *
 */

class Oauth
{
    public static function oauthToken(array $data)
    {
        try {
            $oauth = $data['data']['oauth'];
            $platform_name = $data['data']['platform_name'];
            if (method_exists(__CLASS__, $platform_name)) {
                //根据平台名称刷新对应的token
                if (!isset($data['data']['type'])) {
                    //有刷新码，直接获取访问令牌
                    return self::$platform_name($oauth);
                } else {
                    //如果有授权码，则使用授权码刷新token
                    if ($oauth['oauth_token']) {
                        //根据授权码，获取刷新令牌
                        $refresh_platform = 'refresh' . $platform_name;
                        if (method_exists(__CLASS__, $refresh_platform)) {
                            return self::$refresh_platform($oauth);
                        }
                    }
                }
            }
        } catch (Exception $e) {
            throw $e;
        }

        return false;
    }


    //刷新亚马逊token
    protected static function AM($oauth)
    {
        $client = new \Sdk\Amazon\Api\Tokens($oauth);
        $queryParams = [
            'grant_type' => 1,
            'code' => 1,
            'redirect_uri' => 1,
            'client_id' => 1,
            'client_secret' => 1,
        ];
        $client->refreshAccessToken($queryParams);
        $dir = ROOT_DIR . '/data/amazon/';
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        fopen($dir . "/token.txt", "w");
        $tokenStorage = new \DoubleBreak\Spapi\SimpleTokenStorage($dir . '/token.txt');
        $signer = new \DoubleBreak\Spapi\Signer();
        $credentials = new \DoubleBreak\Spapi\Credentials($tokenStorage, $signer, $config);
        $cred = $credentials->getCredentials();
        if (!empty($cred)) {
            try {
                $data['access_token'] = $cred['access_token'];
                $data['session_token'] = $cred['sts_credentials']['session_token'];
                $data['access_key'] = $cred['sts_credentials']['access_key'];
                $data['secret_key'] = $cred['sts_credentials']['secret_key'];
                //有效期
                $expires_time = date('Y-m-d H:i:s', time() + 3600);
                //更新账号授权信息
                $oauth = json_encode($data);
                $updated_at = date('Y-m-d H:i:s');
                $sql = "UPDATE account SET oauth = '{$oauth}', expires_time = '{$expires_time}', 
                    updated_by = 1, updated_at = '{$updated_at}' WHERE 1 AND id = '{$data['id']}'";
                L::sql($sql);
                return true;
            } catch (Exception $exp) {
                trigger_error(json_encode($exp, JSON_UNESCAPED_UNICODE));
                return false;
            }
        }

        return false;
    }
}